$(function () {
    obj.init();
});
var obj = new function () {
   'use strict';
   return {
       currentList:null,
       oldList:null,
       carId:null,
       page: 1,
       rows: 10,
       footer:null,
       init:function () {
           this.carId       = addressUri.getParam('iCarId');
           this.currentList = $('#currentList');
           this.oldList     = $('#oldList');
           this.funObj().loadData({url:baseUrl+'mobile/shortcut/getUseCar',data:{carId:this.carId},type:1});
           this.funObj().loadData({url:baseUrl+'mobile/shortcut/getUseCar/true',data:{carId:this.carId,page:this.page,rows:this.rows},type:2});
           this.funObj().appendFooter();
           this.listener();
           this.funObj().bindBack();
       },
       listener:function () {
           var $this = this;
           $('#prePage').click(function () {
               $this.funObj().prevPage();
           });
           $('#nextPage').click(function () {
               $this.funObj().nextPage();
           });
       },
       funObj:function () {
           var $this = this;
           return {
               initList:function (data,type) {
                   if(type === 1){
                       var target = $this.currentList;
                       var str = '<li class="m-list-group">当前</li>';
                   }else {
                       var target = $this.oldList;
                       var str = '<li class="m-list-group">历史</li>';
                   }
                   target.empty();
                   $.each(data,function (i,item) {
                       var left ='';
                       if(item.tStartTime != null){
                           if(item.tRealStart !=null){
                               left = '<div class="m-right"><span class="m-badge c1">'+item.tRealStart+'</span></div>';
                           }else {
                               left = '<div class="m-right"><span class="m-badge c2">'+item.tStartTime+'</span></div>';
                           }
                       }
                       str += '<li><a  nodeId="'+item.iUCId+'" href="javascript:void(0)" onclick="openit(this)"><div class="content" style="width: 210px;">'+item.sApplyName+'</div>'+left+'</a></li>';
                   });
                   if(type == 2 && data.length < $this.rows){
                       $('#nextPage').hide();
                   }
                   if(data.length > 0){
                       target.append(str);
                   }else {
                       str += '<li>没有记录</li>';
                       target.append(str);
                       if(type == 2 ){
                           $('#nextPage').hide();
                       }
                   }
               },
               loadData:function (params) {
                   $.ajax({
                       url:params.url,
                       dataType:'json',
                       method:'post',
                       data:params.data,
                       beforeSend: function () {
                           console.log('aaa');
                           window.top.showOverlay();
                       },
                       success:function (data) {
                            $this.funObj().initList(data,params.type);
                       },
                       complete: function () {//完成响应
                           console.log('bb');
                           window.top.hideOverlay();
                       }
                   })
               },
               appendFooter:function () {
                   $('#page').text($this.page);
                   $('#prePage').hide();
               },
               prevPage:function () {
                   if($this.page <= 2){
                       $('#prePage').hide();
                   }
                   $this.page -= 1;
                   $this.funObj().loadData({url:baseUrl+'mobile/shortcut/getUseCar/true',data:{carId:$this.carId,page:$this.page,rows:$this.rows},type:2});
                   $('#page').text($this.page);
                   $('#nextPage').show();
               },
               nextPage:function () {
                   $this.page += 1;
                   $this.funObj().checkEnd({url:baseUrl+'mobile/shortcut/getUseCar/true',data:{carId:$this.carId,page:$this.page+1,rows:$this.rows},type:2});
                   $this.funObj().loadData({url:baseUrl+'mobile/shortcut/getUseCar/true',data:{carId:$this.carId,page:$this.page,rows:$this.rows},type:2});
                   $('#page').text($this.page);
                   $('#prePage').show();
               },
               checkEnd:function (params) {
                   $.ajax({
                       url:params.url,
                       dataType:'json',
                       method:'post',
                       data:params.data,
                       success:function (data) {
                           if(data.length <= 0 || data.length <$this.rows){
                               $('#nextPage').hide();
                           }
                       },
                       error:function () {
                           $('#nextPage').hide();
                       }
                   })
               },
               bindBack:function () {
                   window.top.bindBack(function () {
                       var title = '车辆';
                       var href  = baseUrl+'mobile/shortcut/carList';
                       $('#targetFrame',window.top.document).attr('src',href);
                       window.top.setTitle(title);
                   });
               }
           }
       }
   }
};

function openit(target){ //跳转页面
    window.top.registerBack();//注册返回
    var id   = $(target).attr('nodeId');
    var carPlate = $(target).find('.content').text();
    $('#targetFrame',window.parent.document).attr('src',baseUrl+'mobile/shortcut/useCar?iUCId='+id);
    window.parent.setTitle(carPlate);
    window.parent.goTarget();
}